// pages/tobeSupplier/tobeSupplier.js
var url_js = require("../../utils/url.js");
var common_js = require("../../utils/common.js");
var ubt_js = require("../../utils/ubt.js");
var name_js = require("../../utils/ubtButtonName.js");
var api = require("../../utils/apiRequest.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    submitInfo: {
      name: '',
      phone: '',
      ztName: '',
      category: '',
      score: 0,
      isSureEnable: false
    },
    categoryList: null,
    indexInfo: { //记录当前选择的index
      categoryIndex: 0
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //业务埋点
    ubt_js.myPageView(common_js.currentPageName(), options);
    //1.禁止转发
    wx.hideShareMenu();
    //2.获取分类
    this.requestToGetSupplierCateogry();
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    // 业务埋点
    ubt_js.myPageViewOut(common_js.currentPageName());
  },


  //获取供应商主营类目
  requestToGetSupplierCateogry: function() {
    let data = {};
    api.wxRequest(url_js.shopSupplierCateogryUrl, data).then(res => {
      this.handleSupplierCateogryData(res);
    }).catch(err => {
      common_js.log(err);
    });
  },

  //处理获取的供应商分类
  handleSupplierCateogryData: function(res) {
    let tempList = res.result;
    if (tempList.length > 0) {
      this.setData({
        categoryList: tempList
      });
    } else {
      common_js.showToast(res.message);
    }
  },

  //真实姓名
  onName: function(e) {
    var tmp = e.detail.content;
    var tmpSubmitInfo = this.data.submitInfo;
    if (tmpSubmitInfo.name.length == 0 && tmp.length > 0) {
      tmpSubmitInfo.score++;
    } else if (tmpSubmitInfo.name.length > 0 && tmp.length == 0) {
      tmpSubmitInfo.score--;
    }
    tmpSubmitInfo.name = tmp;
    if (tmpSubmitInfo.score == 4) {
      tmpSubmitInfo.isSureEnable = true;
    } else {
      tmpSubmitInfo.isSureEnable = false;
    }
    this.setData({
      submitInfo: tmpSubmitInfo
    });
  },

  //手机号
  onPhone: function(e) {
    var tmp = e.detail.content;
    var tmpSubmitInfo = this.data.submitInfo;
    if (tmpSubmitInfo.phone.length == 0 && tmp.length > 0) {
      tmpSubmitInfo.score++;
    } else if (tmpSubmitInfo.phone.length > 0 && tmp.length == 0) {
      tmpSubmitInfo.score--;
    }
    tmpSubmitInfo.phone = tmp;
    if (tmpSubmitInfo.score == 4) {
      tmpSubmitInfo.isSureEnable = true;
    } else {
      tmpSubmitInfo.isSureEnable = false;
    }
    this.setData({
      submitInfo: tmpSubmitInfo
    });
  },

  //供应商主体名称
  onZtName: function(e) {
    var tmp = e.detail.content;
    var tmpSubmitInfo = this.data.submitInfo;
    if (tmpSubmitInfo.ztName.length == 0 && tmp.length > 0) {
      tmpSubmitInfo.score++;
    } else if (tmpSubmitInfo.ztName.length > 0 && tmp.length == 0) {
      tmpSubmitInfo.score--;
    }
    tmpSubmitInfo.ztName = tmp;
    if (tmpSubmitInfo.score == 4) {
      tmpSubmitInfo.isSureEnable = true;
    } else {
      tmpSubmitInfo.isSureEnable = false;
    }
    this.setData({
      submitInfo: tmpSubmitInfo
    });
  },

  //主营类目
  onCategory: function(e) {
    //index
    var index = parseInt(e.detail.detail.value);
    var tempIndexInfo = this.data.indexInfo;
    tempIndexInfo.categoryIndex = index;
    //item
    var item = this.data.categoryList[index];
    var tmp = item.categoryName;
    //submitInfo
    var tmpSubmitInfo = this.data.submitInfo;
    if (tmpSubmitInfo.category.length == 0 && tmp.length > 0) {
      tmpSubmitInfo.score++;
    } else if (tmpSubmitInfo.category.length > 0 && tmp.length == 0) {
      tmpSubmitInfo.score--;
    }
    tmpSubmitInfo.category = tmp;
    if (tmpSubmitInfo.score == 4) {
      tmpSubmitInfo.isSureEnable = true;
    } else {
      tmpSubmitInfo.isSureEnable = false;
    }
    this.setData({
      submitInfo: tmpSubmitInfo
    });
  },

  //提交
  onSure: function() {
    //1.手机号
    if (!common_js.isPhone(this.data.submitInfo.phone)) {
      common_js.showToast('请输入正确的手机号!');
      return;
    }
    this.requestToSupplierApply();
    //
    //埋点
    ubt_js.myClickButton(common_js.currentPageName(), name_js.tobeSupplierDone);
  },

  //成为供应商
  requestToSupplierApply: function() {
    let cateItem = this.data.categoryList[this.data.indexInfo.categoryIndex];
    let data = {
      categories: [cateItem.categoryName],
      mobile: this.data.submitInfo.phone,
      realName: this.data.submitInfo.name,
      supplierName: this.data.submitInfo.ztName
    };
    api.wxRequest(url_js.shopSupplierApplyUrl, data).then(res => {
      this.handleSupplierApplyData(res);
    }).catch(err => {
      common_js.log(err);
    });
  },

  //处理成为供应商
  handleSupplierApplyData: function(res) {
    common_js.showModal("提交成功！官方人员将主动电话联系您，以便核实更多信息。请保持您的手机畅通！", null, null, null, null, () => {
      wx.navigateBack({});
    });
  },
})